package com.wyy.treeview.ui;

import java.io.InputStream;
import java.util.List;

import org.apache.http.util.EncodingUtils;

import com.wyy.treeview.core.DaoHelper;
import com.wyy.treeview.core.GsonUtil;
import com.wyy.treeview.core.Location;
import com.wyy.treeview.R;
import com.wyy.treeview.R.id;
import com.wyy.treeview.R.layout;
import com.wyy.treeview.R.menu;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;

public class TreeActivity extends ActionBarActivity {
	private boolean isSuccess = false;
	private ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		progressBar.setVisibility(View.VISIBLE);
		//读取assets下的json文件数据并保存到数据库
		saveLocData(getLocJson());
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			progressBar.setVisibility(View.GONE);
			switch (msg.what) {
			case 1:
				if (isSuccess) {
					//树展示
					getSupportFragmentManager()
							.beginTransaction()
							.replace(R.id.container_main,
									LocationFragment.newInstance()).commit();
				}
				break;

			default:
				break;
			}
		};

	};

	private String getLocJson() {

		String fileName = "location.txt"; // 文件名字
		String res = "";
		try {
			// 得到资源中的asset数据流
			InputStream in = getResources().getAssets().open(fileName);
			int length = in.available();
			byte[] buffer = new byte[length];

			in.read(buffer);
			in.close();
			res = EncodingUtils.getString(buffer, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	private void saveLocData(final String jsonData) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				if (!GsonUtil.isValidJson(jsonData)) {
					return;
				} else {
					List<Location> list = GsonUtil.getParserList(jsonData,
							"location", Location.class);
					DaoHelper daoHelper = new DaoHelper<Location>(
							TreeActivity.this, Location.class);
					daoHelper.deleteAll();
					isSuccess = daoHelper.createOrUpdate(list);
					if (isSuccess) {
						handler.sendEmptyMessage(1);
					}

				}
			};
		}.start();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
